package com.mtw.bbs.fileAdmin.strategy;



import com.mtw.bbs.fileAdmin.constant.UploadStrategyEnum;
import com.mtw.bbs.fileAdmin.pojo.entity.FileEntity;
import com.mtw.bbs.fileAdmin.pojo.entity.FileShard;
import com.mtw.bbs.fileAdmin.util.QiniuTemplate;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;

/**
 * minio文件上传
 */
@Component
@RequiredArgsConstructor
public class QiniuUpload implements UploadStrategy{


    private final QiniuTemplate qiniuTemplate;


    @Override
    public String strategy() {
        return UploadStrategyEnum.QINIU.getCode();
    }

    /**
     * 文件上传
     * @return
     */
    @SneakyThrows
    @Override
    public boolean upload(MultipartFile file, FileEntity fileInfo) {
        return qiniuTemplate.uploadFile(file.getInputStream(), fileInfo.getInnerPath());
    }


    /**
     * 上传
     */
    @Override
    @SneakyThrows
    public boolean simpleUpload(File file, String path) {
        return  qiniuTemplate.uploadFile(file, path);
    }


    /**
     * 分片上传
     * @return
     */
    @Override
    public boolean shardUpload(MultipartFile file, FileShard fileShard){
        return  qiniuTemplate.shardUpload(fileShard,file);
    }

    /**
     * 合并文件
     * @return
     */
    @Override
    public boolean mergeFile(FileShard fileShard) {
        return qiniuTemplate.mergeFile(fileShard);
    }


    /**
     * 获取文件url
     * @param path
     * @return
     */
    @SneakyThrows
    @Override
    public String getFileUrl(String path)  {
        return qiniuTemplate.getFileUrl(path);
    }


    /**
     * 获取文件流
     */
    @Override
    public InputStream getFileStream(String path) {
        return null;
    }
}
